package com.ptp.backendcertms.basiccompnents.exception;

import com.ptp.backendcertms.basiccompnents.data.dao.entity.ExceptionPo;
import com.ptp.backendcertms.basiccompnents.data.dao.service.IExceptionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 异常信息处理实现
 *
 * @author scc
 * @date 2025/5/1
 */
@Service
public class ExceptionHandler implements IExceptionHandler {
    @Resource
    private IExceptionService exceptionService;

    @Async("asyncEventExecutor")
    @Override
    public void addExceptionMsg(String name, String traceLogId, String exceptionMsg) {
        // 限制最大traceId长度为100个字符
        traceLogId = StringUtils.substring(traceLogId, 0, 100);
        ExceptionPo exceptionPo = new ExceptionPo();
        exceptionPo.setRequestName(name);
        exceptionPo.setTraceLogId(traceLogId);
        exceptionPo.setExceptionMessage(exceptionMsg);
        exceptionService.saveExceptionInfo(exceptionPo);
    }
}
